package com.youlu.campus.service.recycle.service.impl;

import com.youlu.campus.service.recycle.convert.RecycleItemConvert;
import com.youlu.campus.entity.recycle.dto.RecycleItemSaveDTO;
import com.youlu.campus.entity.recycle.dto.RecycleItemSearchDTO;
import com.youlu.campus.service.recycle.service.RecycleItemInfoService;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.recycle.RecycleItemInfo;
import com.youlu.campus.service.course.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author : [dengbo]
 * @className : RecycleItemInfoServiceImpl
 * @description : [商品实现类]
 * @createTime : [2022/6/23 14:23]
 */
@Service
@Slf4j
public class RecycleItemInfoServiceImpl implements RecycleItemInfoService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RecycleItemConvert recycleItemConvert;

    @Override
    public Page<RecycleItemInfo> search(RecycleItemSearchDTO dto) {
        QueryWrapper query = new QueryWrapper();
        if (StringUtils.isNotBlank(dto.getName())) {
            query.like("name", dto.getName());
        }
        if(StringUtils.isNotBlank(dto.getOnSale())){
            query.where("onSale", Integer.valueOf(dto.getOnSale()));
        }
        query.addCriteria(Criteria.where("deleted").ne(true));
        long count = mongoTemplate.count(query, RecycleItemInfo.class);
        query.with(Sort.by(Sort.Order.desc("id")));
        PageRequest pageRequest = PageRequest.of(dto.getPage() - 1, dto.getPageSize());
        List<RecycleItemInfo> data = mongoTemplate.find(query.with(pageRequest), RecycleItemInfo.class);
        return PageableExecutionUtils.getPage(data, pageRequest, () -> count);
    }

    @Override
    public RecycleItemInfo save(RecycleItemSaveDTO dto) {
        RecycleItemInfo itemInfo = null;
        if (StringUtils.isNotBlank(dto.getId())) {
            //修改
            itemInfo = mongoTemplate.findOne(Query.query(Criteria.where("id").is(dto.getId())), RecycleItemInfo.class);
            if (itemInfo == null) {
                throw new BusinessException("商品信息不存在");
            }
        }
        itemInfo = recycleItemConvert.recycleItemToEntity(dto, itemInfo);
        mongoTemplate.save(itemInfo);
        return itemInfo;
    }

    @Override
    public List<RecycleItemInfo> getItemListByIdList(List<String> itemIds) {
        if (CollectionUtils.isEmpty(itemIds)) {
            return Collections.emptyList();
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("id").in(itemIds));
        return mongoTemplate.find(query, RecycleItemInfo.class);
    }
}
